<?php
/**
 * 短链接
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Activity;

use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\ActivityModel;
use App\Models\yb\ShortLinkModel;
use EasyWeChat\Factory;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;


class ShortLinkController extends Controller
{
    use ModelForm;

    protected $header = '短链接管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    /**
     * 详情
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->detailGrid($id));

        });
    }

    public function grid(){

        $grid = new Grid(new ShortLinkModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('title','标题')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('short_link','短链接')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('title','标题');
            });

            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->actions(function($actions){
            $actions->disableDelete();
            $actions->disableView();
            if( $actions->row['status'] == 1){
                $actions->disableEdit();
            }
        });

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new ShortLinkModel());

        $form->select('applte_path_id', '小程序页面')->options(DataDictionaryModel::ShortLinkOptions)->required();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        $form->saved(function (Form $form) {
            $config = [
                'app_id' => 'wx9a67fcdacb9b9256',
                'secret' => '81fad759102bd90044c154fedd40d3bc',
            ];
            $path_data = DataDictionaryModel::ShortLink;

            $app = Factory::officialAccount($config);

            $bb = $app->base->httpPostJson('wxa/generate_urllink',[
                'path' => $path_data[$form->model()->applte_path_id-1]['path'],
                'is_expire' => true,
                'expire_type' => 1,
                'expire_interval' => 1
            ]);

            ShortLinkModel::where(['id'=>$form->model()->id])->update([
                'title' => $path_data[$form->model()->applte_path_id-1]['title'],
                'applt_path' => $path_data[$form->model()->applte_path_id-1]['path'],
                'short_link' => $bb['url_link'],
            ]);

        });

        return $form;
    }


}
